<?php
/**
 * @权限类
 * 
 * 一、用户类
 * 1、登录
 * 2、注册
 * 3、忘记密码
 * 4、修改资料 
 * 5、获取基本信息
 * @作者 
 * 万全强（php）
 */
namespace app\controller;

use app\controller\HomeBase;
use think\facade\Db;
use app\model\Admin;


use app\model\UserModel;
use app\model\Role;


class User extends HomeBase
{

    private $userModel;

    public function __construct(){
               
        parent::__construct();        
         $this->userModel=new UserModel();

    }
    /**
     * 登录
     */
    public function Login(){
        $param=request()->param();
        
        $login_type=request()->param('login_type',1,'intval');
        //表单验证     
        try {
        	if($login_type==1){
        		$re=$this->validate($param,'app\validate\UserLoginPwd');       		
        	}
        	if($login_type==2){
        		$re=$this->validate($param,'app\validate\UserLoginCode');
        	}
        } catch (\Exception $e) {       
            return json(['status'=>0,'msg'=>$e->error,'data'=>[]]);
        }
        $users=$this->userModel->GetUserByMobile(trim($param['mobile']),$param['id'],2);
        if(empty($users)){
            return json(['status'=>0,'msg'=>'用户不存在','data'=>[]]);
        }
        
        //p($users->password);
        if($login_type==1){
        	if(($users->password)!=md5(trim($param['password']))){
        		return json(['status'=>0,'msg'=>'用户密码错误','data'=>[]]);
        	}
        }
        if($login_type==2){
        	$userFlag = $this->getBusiness('Mobile')->checkmobileCode($param['mobile'],$param['captcha'],2);
        	if (!$userFlag){
        		return json(['status'=>0,'msg'=>'手机验证码不正确','data'=>[]]);
        	}
        	if ($userFlag === 2){
        		return json(['status'=>0,'msg'=>'手机验证码输入错误次数太多，请稍后再试','data'=>[]]);
        	}
        }
        
        
        if($users['status']==0){
            return json(['status'=>0,'msg'=>'您的账号已经禁用，请联系管理员','data'=>[]]);
        }
        return json(['status'=>1,'msg'=>'登录成功','data'=>['token'=>signToken($users['id'])]]);
     }
       
    /**
     * 注册
     */
    public function Register(){
       
		$param['mobile'] = request()->param('mobile');
		$param['password'] = request()->param('password');
		$param['captcha'] = request()->param('captcha');
		$param['id'] = request()->param('id',0,'intval');
        try {
            $re=$this->validate($param,'app\validate\UserRegister');
         } catch (\Exception $e) {       
             return json(['status'=>0,'msg'=>$e->error,'data'=>[]]);
         }
         
        //判断平台是否存在
        $Role=new Role();
        $info=$Role->detail($param['id']);
        if(empty($info)||$info['status']!=1){
        	return json(['status'=>0,'msg'=>'平台不存在','data'=>[]]);
        }

        $result = $this->getBusiness('User')->register($param);
        if($result['status']==1){
            return json(['status'=>1,'msg'=>'注册成功','data'=>[]]);
        }else{
            return json(['status'=>0,'msg'=>$result['msg'],'data'=>[]]);
        }
        

    }
    /**
     * 忘记密码
     */
    public function ForgetPwd(){

    }
    /**
     * 修改资料
     */
    public function UpdateUser(){
    	$param=request()->param();
    	
    	//表单验证
    	try {
    		//$re=$this->validate($param,'app\validate\AdminValidate');
    	} catch (\Exception $e) {
    		//return json(['status'=>0,'msg'=>$e->error,'data'=>[]]);
    	}   	
    	//管理表
    	$AdminModel=new Admin();
    	$AdminModel->EditData($param,$this->user_id);
    	return json(['status'=>1,'msg'=>'操作成功','data'=>[]]);
    	
    }
    /**
     * 获取基本信息
     */
    public function GetUseInfo(){
    	$AdminModel=new Admin();
    	$result=$AdminModel->detail($this->user_id);
    	return json(['status'=>1,'msg'=>'操作成功','data'=>$result]);
    	
    }
   
}